/*
 * Copyright (c) 2013-2015 Charkey. All rights reserved.
 *
 * This software is the confidential and proprietary information of Charkey.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with Charkey.
 *
 * Charkey MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * Charkey SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package cn.simastudio.talos.inter.controller.web;

import cn.simastudio.talos.inter.model.inst.Member;
import cn.simastudio.talos.inter.service.inst.MemberService;
import cn.simastudio.talos.mybatis.pagehelper.PageHelper;
import cn.simastudio.talos.mybatis.pagehelper.PageInfo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Usage:
 *
 * @author LabaiJos
 * @date 2015/6/6 16:55
 */
@RestController
@RequestMapping("/console/members")
public class MemberController {

    @Autowired
    MemberService memberService;

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public List<Member> searchMember(@RequestParam(value = "start", defaultValue = "0", required = false) int start,
                                     @RequestParam(value = "limit", defaultValue = "10", required = false) int size,
                                     @RequestParam(value = "name", defaultValue = "", required = false) String name) {
        // TODO:参数取值
        Member member = new Member();
        if (!name.trim().equals("")) {
            member.setUsername(name.trim());
        }
        int page = start / size + 1;
        PageHelper.startPage(page, size);
        List<Member> memberList = memberService.getMemberList(member);
        return memberList;
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public JSONObject getMembers(@RequestParam(value = "iDisplayStart", defaultValue = "0", required = false) int start,
                                 @RequestParam(value = "iDisplayLength", defaultValue = "10", required = false) int size,
                                 @RequestParam(value = "sSearch", defaultValue = "", required = false) String name) {
        // TODO:参数取值
        Member member = new Member();
        if (!name.trim().equals("")) {
            member.setUsername(name.trim());
        }
        int page = start / size + 1;
        PageHelper.startPage(page, size);
        List<Member> memberList = memberService.getMemberList(member);
        @SuppressWarnings("unchecked")
        PageInfo pageInfo = new PageInfo(memberList);
        JSONObject data = new JSONObject();
        data.put("recordsTotal", pageInfo.getTotal());
        data.put("recordsFiltered", pageInfo.getTotal());
        data.put("aaData", memberList);
        return data;
    }
}
